<html>
<body>

<?php

require_once("Bootstrap.php");

function drawDefault()
{
	$personDao = new GenericDao("Person");
	$userAccountDao = new GenericDao("UserAccount");
	$stateDao = new GenericDao("State");
	$countryDao = new GenericDao("Country");
	
	echo 'Create Country:<br/>';
	echo '<form>';
	echo '<input type="hidden" name="process" Value="addCountry" />';
	echo 'Name: <input type="text" name="Name" Value="Australia" /><br/>';
	echo '<input type="submit" value="Submit" />';
	echo '</form><br/><br/>';	

	echo '<table border="1">';
	echo '<tr>';
	echo '<th>Name</th>';
	echo '</tr>';
	$countries = $countryDao->findAll(); 
	foreach($countries as $country)
	{
		echo '<tr>';
		echo '<td>'.$country->getName().'</td>';
		echo '</tr>';		
	}
	echo '</table><br/><br/>';		
	
	echo 'Create State:<br/>';
	echo '<form>';
	echo '<input type="hidden" name="process" Value="addState" />';
	echo 'Name: <input type="text" name="Name" Value="Victoria" /><br/>';
	echo 'Country: <select name="Country">';
	$countries = $countryDao->findAll(); 
	foreach($countries as $country)
	{
		echo '<option value="'.$country->getId().'">'.$country.'</option>';
	}
	echo '</select><br/>';	
	echo '<input type="submit" value="Submit" />';
	echo '</form><br/><br/>';	

	echo '<table border="1">';
	echo '<tr>';
	echo '<th>Name</th>';
	echo '<th>Country</th>';
	echo '</tr>';
	$states = $stateDao->findAll(); 
	foreach($states as $state)
	{
		echo '<tr>';
		echo '<td>'.$state->getName().'</td>';
		echo '<td>'.$state->getCountry().'</td>';
		echo '</tr>';		
	}
	echo '</table><br/><br/>';	
	
	echo 'Create Person:<br/>';
	echo '<form>';
	echo '<input type="hidden" name="process" Value="addPerson" />';
	echo 'FirstName: <input type="text" name="FirstName" Value="Bob" /><br/>';
	echo 'LastName: <input type="text" name="LastName" Value="Smith" /><br/>';
	echo 'Address:<br/>';
	echo 'Line1: <input type="text" name="Line1" Value="12 Bonce St" /><br/>';
	echo 'Line2: <input type="text" name="Line2" Value="" /><br/>';
	echo 'Suburb: <input type="text" name="Suburb" Value="Glen Waverley" /><br/>';
	echo 'Postcode: <input type="text" name="Postcode" Value="3150" /><br/>';
	echo 'State: <select name="State">';
	$states = $stateDao->findAll(); 
	foreach($states as $state)
	{
		echo '<option value="'.$state->getId().'">'.$state.'</option>';
	}
	echo '</select>';	

	echo 'Country: <select name="Country">';
	$countries = $countryDao->findAll(); 
	foreach($countries as $country)
	{
		echo '<option value="'.$country->getId().'">'.$country.'</option>';
	}
	echo '</select><br/>';	
	echo '<input type="submit" value="Submit" />';
	echo '</form><br/><br/>';
	
	echo '<table border="1">';
	echo '<tr>';
	echo '<th>Firstname</th>';
	echo '<th>Lastname</th>';
	echo '<th>UserAccount</th>';
	echo '<th>Address</th>';
	echo '</tr>';
	$people = $personDao->findAll(); 
	foreach($people as $person)
	{
		echo '<tr>';
		echo '<td>'.$person->getFirstName().'</td>';
		echo '<td>'.$person->getLastName().'</td>';
		echo '<td>'.$person->getUserAccount().'</td>';
		echo '<td>'.$person->getAddress().'</td>';
		echo '</tr>';		
	}
	
	echo '</table><br/><br/>';
	
	echo 'Create UserAccount:<br/>';
	echo '<form>';
	echo '<input type="hidden" name="process" Value="addUserAccount" />';
	echo 'Username: <input type="text" name="Username" Value="admin" /><br/>';
	echo 'Password: <input type="text" name="Password" Value="admin" /><br/>';
	echo 'Person: <select name="Person">';
	$people = $personDao->findAll(); 
	foreach($people as $person)
	{
		echo '<option value="'.$person->getId().'">'.$person->getFirstName()." ".$person->getLastName().'</option>';
	}
	echo '</select>';
	echo '<input type="submit" value="Submit" />';
	echo '</form><br/><br/>';
	
	echo '<table border="1">';
	echo '<tr>';
	echo '<th>UserName</th>';
	echo '<th>Password</th>';
	echo '<th>Person</th>';
	echo '</tr>';
	$userAccounts = $userAccountDao->findAll(); 
	foreach($userAccounts as $userAccount)
	{
		echo '<tr>';
		echo '<td>'.$userAccount->getUserName().'</td>';
		echo '<td>'.$userAccount->getPassword().'</td>';
		echo '<td>'.$userAccount->getPerson().'</td>';
		echo '</tr>';		
	}
	
	echo '</table><br/><br/>';	
		
}

//Dao::$debug = true;
switch($_GET['process'])
{
	case "addPerson":
		$person = new Person();
		$person->setFirstName($_GET['FirstName']);
		$person->setLastName($_GET['LastName']);
		$address = new Address();
		$address->setLine1($_GET["Line1"]);
		$address->setLine2($_GET["Line2"]);
		$address->setSuburb($_GET["Suburb"]);
		$address->setPostCode($_GET["Postcode"]);
		$stateDao = new GenericDao("State");
		$state = $stateDao->findById($_GET["State"]);
		$address->setState($state);
		$countryDao = new GenericDao("Country");
		$country = $countryDao->findById($_GET["Country"]);
		$address->setCountry($country);
		$addressDao = new GenericDao("Address");
		$addressDao->save($address);
		$person->setAddress($address);
		$dao = new GenericDao("Person");
		$dao->save($person);
		drawDefault();
		break;
	case "addUserAccount":
		$userAccount = new UserAccount();
		$userAccount->setUserName($_GET["Username"]);
		$userAccount->setPassword($_GET["Password"]);
		$dao = new GenericDao("Person");
		$person = $dao->findById($_GET["Person"]);
		$userAccount->setPerson($person);
		$dao->save($userAccount);
		drawDefault();
		break;
	case "addCountry":
		$country = new Country();
		$country->setName($_GET["Name"]);
		$dao = new GenericDao("Country");
		$dao->save($country);
		drawDefault();
		break;
	case "addState":
		Dao::$debug = true;
		$state = new State();
		$state->setName($_GET["Name"]);
		$dao = new GenericDao("Country");
		$country = $dao->findById($_GET["Country"]);
		$state->setCountry($country);
		$dao = new GenericDao("State");
		$dao->save($state);
		drawDefault();
		break;
	default:
		drawDefault();
		break;
}

?>
</body>
</html>